<?php

namespace app\admin\controller\vv;

use app\common\controller\Backend;
use vv\service\Shop;

class Supply extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \vv\model\Shop();
    }

    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if (!$this->request->isAjax()) {
            return $this->fetch();
        }
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model->where($where)->where(function ($query) {
            $query->where('shoptype', 2)->whereOr('apply', 1);
        })->order($sort, $order)->paginate($limit);
        return json([
            'total' => $list->total(),
            'rows'  => $list->items()
        ]);
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$this->request->isPost()) {
            $row['v'] = $row['shoptype'] == 2 ? 1 : 0;
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if ($params['v'] == 1) {
            Shop::supply_enable($row['id']);
        } else {
            Shop::supply_disable($row['id']);
        }
        $this->success();
    }
}